<template>
  <div class="login">
    <el-card class="login-card" :body-style="{ padding: '60px' }">
      <div
        slot="header"
        style="font-size: 20px; font-weight: bold; color: #666666"
      >
        <!-- 设置名称为header的插槽，该插槽在卡片的顶部 -->
        OA云协作
      </div>
      <el-form
        ref="loginForm"
        :model="user"
        :rules="loginFormRules"
        style="margin-top: 0px"
        size="large"
      >
        <el-form-item style="height: 60px" prop="u_id">
          <el-input v-model="user.u_id" placeholder="账号"></el-input>
        </el-form-item>

        <el-form-item style="height: 60px" prop="u_pwd">
          <el-input
            type="password"
            v-model="user.u_pwd"
            :show-password="true"
            placeholder="密码"
          ></el-input>
        </el-form-item>

        <el-form-item style="height: 30px">
          <el-button type="primary" @click="login" style="width: 100%"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { apiLogin, apiLogin2 } from "../api/login";
import { putToken} from "@/common/token";
import { loadMenus } from "@/common/auth";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      user: {}, //登录表单数据
      loginFormRules: {
        //定义验证规则
        u_id: [{ required: true, message: "请输入账号！", trigger: "blur" }],
        u_pwd: [{ required: true, message: "请输入密码！", trigger: "blur" }],
      },
    };
  },

  methods: {
    ...mapActions(["putUserMenus"]),
    login() {
      console.log(process.env.VUE_APP_INFO);
      this.$refs["loginForm"].validate(async (valid) => {
        if (valid) {
          //验证通过
          //以form-data格式数据发送post请求
          // this.$post('/security/login',this.user)
          //     .then(()=>{//该函数在成功之后执行
          //         this.$router.push('/home');
          //     });
          //await apiLogin(this,this.user);
          try {
            
            let res = await apiLogin2(this.user);            
            putToken(res.data); //在客户端保存token
            loadMenus();
            this.$router.push("/home");
          } catch (error) {
           
            console.log(error);
          }
          
        } 

        return false; //阻止表单事件的默认行为
      });
    },
  },
};
</script>

<style scoped>
.login {
  position: fixed; /* 固定定位，相对于html */
  left: 0px; /* 元素的左边于窗口的最左侧距离为0 */
  right: 0px;
  top: 0px;
  bottom: 0px;

  /*  background-color: red; */

  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.login-card {
  width: 400px;
  margin-bottom: 100px;
}
</style>